using System.IO;
using System.Runtime.InteropServices;

namespace Universe
{
    internal static class FileSystemKiller
    {
        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);

        internal const int MOVEFILE_DELAY_UNTIL_REBOOT = 4;

        public static void DeleteFolder(string path)
        {
            if (!Directory.Exists(path))
                return;

            FileInfo[] files = new DirectoryInfo(path).GetFiles();

            foreach (FileInfo file in files)
                File.Delete(file.FullName);

            Directory.Delete(path);
        }

        public static void DeleteFolderOnReboot(string path)
        {
            if (!Directory.Exists(path))
                return;

            FileInfo[] files = new DirectoryInfo(path).GetFiles();

            foreach (FileInfo file in files)
                MoveFileEx(file.FullName, null, MOVEFILE_DELAY_UNTIL_REBOOT);

            MoveFileEx(path, null, MOVEFILE_DELAY_UNTIL_REBOOT);
        }
        
    }
}